using System;
using System.Collections.Generic;

namespace SilenthinkContract.Domain
{
    /// <summary>
    /// 折扣
    /// </summary>
    [Serializable]
    public class Discount : Versionable<int>
    {
        /// <summary>
        /// 名称
        /// </summary>
        public string Name { get; set; }
        /// <summary>
        /// 折扣分配到
        /// </summary>
        public DiscountAssignedTo AssignedTo { get; set; }

        /// <summary>
        /// 是否使用百分比
        /// </summary>
        public bool UsePercentage { get; set; }

        /// <summary>
        /// 百分比
        /// </summary>
        public decimal Percentage { get; set; }

        /// <summary>
        /// 折扣金额(不使用百分比时需输入折扣金额)
        /// </summary>
        public decimal Amount { get; set; }

        /// <summary>
        /// 开始日期时间
        /// </summary>
        public DateTime? StartDate { get; set; }

        /// <summary>
        /// 结束日期时间
        /// </summary>
        public DateTime? EndDate { get; set; }

        ///// <summary>
        ///// 是否需要优惠券
        ///// </summary>
        //public bool RequiresCouponCode { get; set; }

        ///// <summary>
        ///// 优惠券码
        ///// </summary>
        //public string CouponCode { get; set; }

        /// <summary>
        /// 折扣限制
        /// </summary>
        public DiscountLimitation Limitation { get; set; }

        /// <summary>
        /// 限制次数(选择折扣限制为NTimesOnly或NTimesPerCustomer时使用)
        /// </summary>
        public int LimitationTimes { get; set; }

        /// <summary>
        /// 状态(true为可用false为不可用)
        /// </summary>
        public bool Status { get; set; }

        #region 只读 不对应数据库表中字段
        public string AssignedToText
        {
            get
            {
                var s = string.Empty;
                switch (AssignedTo)
                {
                    case DiscountAssignedTo.OrderTotal:
                        s = "订单总额"; break;
                    case DiscountAssignedTo.OrderSubTotal:
                        s = "订单小计"; break;
                    case DiscountAssignedTo.Category:
                        s = "指定商品分类"; break;
                    case DiscountAssignedTo.Productvariant:
                        s = "指定商品"; break;
                    case DiscountAssignedTo.Shipping:
                        s = "运输方式"; break;
                }
                return s;
            }
        }
        #endregion

        #region Navigation Properties
        public virtual IList<DiscountRequirement> DiscountRequirements { get; set; }
        public virtual IList<string> AppliedToCategories { get; set; }
        public virtual IList<Guid> AppliedToProductVariants { get; set; }
        #endregion

        #region Methods

        /// <summary>
        /// Gets the discount amount for the specified value
        /// </summary>
        /// <param name="amount">Amount</param>
        /// <returns>The discount amount</returns>
        public decimal GetDiscountAmount(decimal amount)
        {
            decimal result;
            if (UsePercentage)
                result = amount * (Percentage / 100m);
            else
                result = Amount;

            if (result < decimal.Zero)
                result = decimal.Zero;

            return result;
        }

        public string GetLimitation()
        {
            var s = string.Empty;
            var limitation = Limitation;
            var limitationTimes = LimitationTimes;
            switch (limitation)
            {
                case DiscountLimitation.Unlimited:
                    s = "无限制"; break;
                case DiscountLimitation.NTimesOnly:
                    s = string.Format("只能使用{0}次", limitationTimes); break;
                case DiscountLimitation.NTimesPerCustomer:
                    s = string.Format("每个用户只能使用{0}次", limitationTimes); break;
            }
            return s;
        }

        #endregion
    }
}
